package com.aoip.telemetry.telemetry.configureui.business.analysis.group.controller;

import com.alibaba.fastjson.JSON;
import com.aoip.telemetry.telemetry.configureui.business.analysis.group.service.ElementAnalysisGroupService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.entity.element.ElementAnalysisGroupConfigEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 元素解析组模块，用于将一个或多个元素组装成解析组
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/12/29 15:25
 */
@RestController
public class ElementAnalysisGroupController extends BaseController {

    @Resource
    private ElementAnalysisGroupService service;

    /**
     * 获取元素解析组配置分页列表
     *
     * @param entity 单个元素解析组
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2021/12/29
     **/
    @GetMapping("/v1/GET/element/group/page")
    public TableDataInfo page(ElementAnalysisGroupConfigEntity entity) {
        startPage();
        List<ElementAnalysisGroupConfigEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 查询所有解析组
     * @author chenbai
     * @date 2021/12/31
     * @param entity 单个元素解析组
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     **/
    @GetMapping("/v1/GET/element/group/list")
    public AjaxResult list(ElementAnalysisGroupConfigEntity entity) {
        // todo 数据量大时，后续可以查询redis 提升查询效率
        List<ElementAnalysisGroupConfigEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 根据元素解析组id，获取该组中所有元素
     *
     * @param analysisGroupId 元素解析组id
     * @return java.util.List<com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity>
     * @author chenbai
     * @date 2021/12/29
     **/
    @GetMapping("/v1/GET/element/group/{analysisGroupId}")
    public AjaxResult elementAnalysisList(@PathVariable String analysisGroupId) {
        return AjaxResult.success(service.elementAnalysisList(analysisGroupId));
    }

    /**
     * 新增或编辑元素解析组中的元素
     * <p>
     * analysisIds         元素数据id集合
     * analysisGroupEntity 解析组实体
     *
     * @return java.util.List<com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity>
     * @author chenbai
     * @date 2021/12/29
     **/
    @Log(title = "解析元素", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/element/group")
    public AjaxResult add(@Validated @RequestBody Map<String, Object> params) {
        List<String> analysisIds = JSON.parseArray(params.get("analysisIds").toString(), String.class);
        String analysisGroupName = params.get("analysisGroupName").toString();
        String funcCode = params.get("funcCode").toString();
        ElementAnalysisGroupConfigEntity analysisGroupEntity = new ElementAnalysisGroupConfigEntity();
        analysisGroupEntity.setAnalysisGroupName(analysisGroupName);
        analysisGroupEntity.setFuncCode(funcCode);
        return AjaxResult.success(service.add(analysisIds, analysisGroupEntity));
    }

    /**
     * 编辑元素解析组中的元素
     * <p>
     * analysisIds         元素数据id集合
     * analysisGroupEntity 解析组实体
     *
     * @return java.util.List<com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity>
     * @author chenbai
     * @date 2021/12/29
     **/
    @Log(title = "解析元素", businessType = BusinessType.INSERT)
    @PutMapping("/v1/PUT/element/group/{analysisGroupId}")
    public AjaxResult edit(@Validated @RequestBody Map<String, Object> params,@PathVariable String analysisGroupId) {
        List<String> analysisIds = JSON.parseArray(params.get("analysisIds").toString(), String.class);
        String analysisGroupName = params.get("analysisGroupName").toString();
        String funcCode = params.get("funcCode").toString();
        ElementAnalysisGroupConfigEntity analysisGroupEntity = new ElementAnalysisGroupConfigEntity();
        analysisGroupEntity.setId(analysisGroupId);
        analysisGroupEntity.setAnalysisGroupName(analysisGroupName);
        analysisGroupEntity.setFuncCode(funcCode);
        return AjaxResult.success(service.edit(analysisIds, analysisGroupEntity));
    }


    /**
     * 初始化测站缓存数据到redis中，数据格式（groupId, List<analysisIdList>），解析组编码，解析组下各个要素id集合
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/element/group/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除解析组以及解析组所有元素
     */
    @Log(title = "解析组以及解析组所有元素", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/element/group/{analysisGroupId}")
    public AjaxResult remove(@PathVariable String[] analysisGroupId) {
        service.delete(analysisGroupId);
        return success();
    }
}
